<?php

namespace App\Game;

use App\Models\Def\MainGamePlat;
use App\Models\QuotaHandicap;
use Carbon\Carbon;

class dt extends Game
{
    private $platName = 'dt';


    public function __construct()
    {

        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }

    //创建用户
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $signtrue = md5($this->business . 'CREATE' . $accountUserName . $password . $this->apiKey . 'CNY');
        $url = $this->gameUrl . '?METHOD=CREATE&BUSINESS=' . $this->business . '&PLAYERNAME=' . $accountUserName . '&PLAYERPASSWORD=' . $password . '&CURRENCY=CNY&SIGNATURE=' . $signtrue;
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['RESPONSECODE']) && $outputarr['RESPONSECODE'] == '00000') {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败'];
        }
    }

    //获取余额
    public function curlGetBalance($accountUserName)
    {
        $signtrue = md5($this->business . 'GETAMOUNT' . $accountUserName . $this->apiKey);
        $url = $this->gameUrl . '?METHOD=GETAMOUNT&BUSINESS=' . $this->business . '&PLAYERNAME=' . $accountUserName . '&SIGNATURE=' . $signtrue;
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['RESPONSECODE']) && $outputarr['RESPONSECODE'] == '00000') {
            return ['success' => true, 'data' => ['balance' => $outputarr['AMOUNT']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '查询失败'];
        }
    }

    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $signtrue = md5($this->business . 'DEPOSIT' . $accountUserName . $price . $this->apiKey . 'CNY');
        $url = $this->gameUrl . '?METHOD=DEPOSIT&BUSINESS=' . $this->business . '&PRICE=' . $price . '&PLAYERNAME=' . $accountUserName . '&TRANSFER_ID=' . $transferid . '&CURRENCY=CNY&SIGNATURE=' . $signtrue;
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['RESPONSECODE'])) {
            if ($outputarr['RESPONSECODE'] == '00000') {
                return ['success' => true, 'data' => ['transferid' => $outputarr['DATA']['TRANSFER_ID'], 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $signtrue = md5($this->business . 'WITHDRAW' . $accountUserName . $price . $this->apiKey . 'CNY');
        $url = $this->gameUrl . '?METHOD=WITHDRAW&BUSINESS=' . $this->business . '&PRICE=' . $price . '&PLAYERNAME=' . $accountUserName . '&TRANSFER_ID=' . $transferid . '&CURRENCY=CNY&SIGNATURE=' . $signtrue;
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['RESPONSECODE'])) {
            if ($outputarr['RESPONSECODE'] == '00000') {
                return ['success' => true, 'data' => ['transferid' => $outputarr['DATA']['TRANSFER_ID'], 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $signtrue = md5($this->business . 'CHECKTRANSFER' . $request->transferid . $this->apiKey);
        $url = $this->gameUrl . '?METHOD=CHECKTRANSFER&BUSINESS=' . $this->business . '&TRANSFER_ID=' . $request->transferid . '&SIGNATURE=' . $signtrue;
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['RESPONSECODE']) && $outputarr['RESPONSECODE'] == '000034') {
            return ['success' => true, 'data' => ['transfer_state'=>1], 'message' => ''];
        } else if (isset($outputarr['RESPONSECODE']) && $outputarr['RESPONSECODE'] == '000035') {
            return ['success' => true, 'data' => ['transfer_state'=>0], 'message' => ''];
        } else {
            return ['success' => false, 'data' => ['transfer_state'=>-1], 'message' => '转帐失败'];
        }
    }

    //踢线

    public function curlKick($request)
    {
        $accountUserName = $request->get('accountUserName');
        $signtrue = md5($this->business . 'LOGINOUT' . $accountUserName . $this->apiKey);
        $url = $this->gameUrl . '?METHOD=LOGINOUT&BUSINESS=' . $this->business . '&PLAYERNAME=' . $accountUserName . '&SIGNATURE=' . $signtrue;
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['RESPONSECODE']) && $outputarr['RESPONSECODE'] == '00000') {
            return ['success' => true, 'data' => [], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '踢线失败'];
        }
    }

    //进入游戏
    public function curlJoinGame($request)
    {

        $accountUserName = $request->input('accountUserName');
        $password = $request->input('password', '');
        $gamecode = $request->input('gamecode', '');
        $demo = $request->input('demo', '');

        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        if (isset($request->quota_handicap)) {
            $oddType = QuotaHandicap::where([
                    ['code', '=', $request->quota_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }
        $lb = isset($oddType) ? $oddType->code : 'L0';

        if (empty($password)) {
            return ['success' => false, 'data' => [], 'message' => '密码为空'];
        }
        if (empty($gamecode)) {
            return ['success' => false, 'data' => [], 'message' => '游戏代码为空'];
        }
        if ($demo == '') {
            return ['success' => false, 'data' => [], 'message' => '试玩代码为空'];
        }

        $signtrue = md5($this->business . 'LOGIN' . $accountUserName . $password . $lb . $this->apiKey);
        $url = $this->gameUrl . '?METHOD=LOGIN&BUSINESS=' . $this->business . '&PLAYERNAME=' . $accountUserName . '&PLAYERPASSWORD=' . $password . '&LB=' . $lb . '&SIGNATURE=' . $signtrue;

        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['RESPONSECODE']) && $outputarr['RESPONSECODE'] == '00000') {
            return ['success' => true, 'data' => ['url' => $outputarr['gameurl'] . '?slotKey=' . $outputarr['slotKey'] . '&language=zh_CN&gameCode=' . $gamecode . '&isfun=' . $demo . '&clientType=0', 'method' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }

    //进入游戏
    public function curlJoinMobileGame($request)
    {
        return $this->curlJoinGame($request);
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $page = 1;
        $pagesize = 5000;

        $starttime = $startTime->toDateTimeString();
        $endtime = $endTime->toDateTimeString();
        $method = 'GETBETDETAIL';
        $signtrue = md5($this->business . $method . $starttime . $endtime . $this->apiKey);
        $data = [];

        do {
            $flag = false;
            $url = $this->gameUrl;
            $params = [
                'METHOD' => $method,
                'BUSINESS' => $this->business,
                'START_TIME' => $starttime,
                'END_TIME' => $endtime,
                'SIGNATURE' => $signtrue,
                'PAGENUMBER' => $page,
                'PAGESIZE' => $pagesize
            ];

            $output = $this->request('GET', $url, $params);
            $output = json_decode($output, true);

            if (isset($output['RESPONSECODE'])) {
                if ($output['RESPONSECODE'] == '00000' && isset($output['BETSDETAILS'])) {
                    $data = array_merge($data, $output['BETSDETAILS']);
                    $flag = true;
                }
            }
            $page++;

        } while ($flag);

        return $data;

    }
}